/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mytest;

/**
 *
 * @author takeshi
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Random;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.util.GenericOptionsParser;
import utility.Tuple;

/**
 *
 * @author takeshi
 */
public class test1 {

    private static String inputfilename = null, outputfilename = null;
    private static String fEncoding = "UTF-8";
    private static int iRandomsize = -1;

    private static void parseParameters(final String[] args) {
        int i = 0;
        for (String par : args) {
            if (par.equalsIgnoreCase("-O")) {
                if (!checkValid(args[i + 1], "Parameter: output file ")) {
                    System.exit(-1);
                }
                outputfilename = args[i + 1].trim();
                System.out.println(outputfilename);

            }else if (par.equalsIgnoreCase("-TP")) {
                System.out.println("Write Tuple");
                break;
            }
            else if (par.equalsIgnoreCase("-I")) {
                if (!checkValid(args[i + 1], "Parameter: input file ")) {
                    System.exit(-1);
                }
                inputfilename = args[i + 1].trim();
                System.out.println(inputfilename);
                break;

            } else if (par.equalsIgnoreCase("-R")) {
                if (!checkValid(args[i + 1], "Parameter: the size of file (MB) ")) {
                    System.exit(-1);
                }

                iRandomsize = Integer.parseInt(args[i + 1].trim());
                if (iRandomsize <= 0) {

                    System.err.println(" file siz should not less than 0");
                    System.exit(-1);
                }
                break;
            }
            i++;

        }

        if (!checkValid(outputfilename, "Parameter: output file ")) {
            System.exit(-1);
        }
    }

    private static boolean checkValid(String in, String explanation) {
        if (in == null || in.isEmpty()) {
            System.err.println(explanation + " is empty");
            return false;
        } else {
            return true;
        }


    }
    // for test
    private static final String[] DATA = {
        "2",
        "-3",
        "-5",
        "8",
        "19",
        "-20"
    };

    // private static ArrayList<String> InDATA= null ;
    public static long readInputData(Configuration conf, FileSystem outfs, Path outpath, final String datafile, final String split) throws IOException {
        long readnum = 0l;
        //     InDATA = new ArrayList<String>();

        // assume the input file is not too large...
        // because it is just for test the correctness
        byte[] buffer = new byte[4096 * 64];

        //  FileSystem fs = FileSystem.get(URI.create(datafile), conf);

        //  Path inpath = new Path(datafile);

        //  FSDataInputStream fin = fs.open(inpath,4096*64);


        StringBuilder text = new StringBuilder();
        String NL = System.getProperty("line.separator");
        Scanner scanner = new Scanner(new File(datafile), fEncoding);
        try {
            while (scanner.hasNextLine()) {
                text.append(scanner.nextLine() + NL);
            }
        } finally {
            scanner.close();
        }
        ///////////////
//       // FSInputStream fin =  (FSInputStream)(in);
//        String inText = new String();
//        char[]  chrBuf = new char[4096*64];
//        int i =0;
//        char tmp = fin.readChar();
//        while(  tmp != -1){
//            chrBuf[i] = tmp;
//            fin.readChar();
//            i++;
//            System.out.println("TEST READ i:" + i +" "+ tmp);
//        }
//        inText.valueOf(chrBuf);
//      //  fin.readFully(0, buffer);
//
//      //  String inText = new String(buffer,fEncoding);
//
//        System.out.print("TEST READ:" + inText);

        String[] strAry = text.toString().split(split.trim());

        LongWritable key = new LongWritable();
        IntWritable value = new IntWritable();
        SequenceFile.Writer writer = null;
        int num = strAry.length;
        System.out.println("the line num is " + num);
        try {
            writer = SequenceFile.createWriter(outfs, conf, outpath,
                    key.getClass(), value.getClass());


            for (int ii = 0; ii < num; ii++) {
                key.set((long) ii);
                System.out.println(strAry[ii]);

                value.set(Integer.valueOf(strAry[ii].trim()));
                System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value);
                writer.append(key, value);
            }
        } finally {
            IOUtils.closeStream(writer);
        }

        readnum = num;

        return readnum;
    }

    public static void printUsage(String extraArgsUsage) {
        System.err.printf("Usage: hadoop jar Write [Options] %s ..\n",
                extraArgsUsage);
        GenericOptionsParser.printGenericCommandUsage(System.err);
    }

    public static void writeRandomInputData(Configuration conf, FileSystem fs, Path outpath, final int sizeMega) throws IOException {

        LongWritable key = new LongWritable();
        IntWritable value = new IntWritable();
        SequenceFile.Writer writer = null;

        try {
            writer = SequenceFile.createWriter(fs, conf, outpath,
                    key.getClass(), value.getClass());

            long num = sizeMega * 1024 * 1024;
            for (long i = 0; i < num; i++) {
                Random rnd = new Random();
                rnd.setSeed(i /127 + 13);
                double r = rnd.nextGaussian();

                key.set(i);
                if (r >= 0)
                    value.set((int)( r * rnd.nextInt() ) );
                else
                    value.set( (-1)*(int)(r*rnd.nextInt() ) );

            //    System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value);
                writer.append(key, value);
            }
        } finally {
            IOUtils.closeStream(writer);
        }

    }

    /**
     * @param args the command line arguments
     * usage [-O output file] [- I input-data file ] [-R size MB]
     */
    public static void main(String[] args) throws IOException {

        parseParameters(args);

        String outuri = outputfilename;

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(outuri), conf);
        Path path = new Path(outuri);

        SequenceFile.Writer writer = null;
        LongWritable key = new LongWritable(0);
        
         try {
                writer = SequenceFile.createWriter(fs, conf, path,
                        key.getClass(), Tuple.class);

                int num = 100;
                for (int i = 0; i < num; i++) {
                    key.set(i);
                    Tuple<Float, Float, Integer> value = new Tuple<Float, Float, Integer>(0f,0f,i);
                    System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value);
                    writer.append(key, value);
                }
            } finally {
                IOUtils.closeStream(writer);
            }
        /*
        if (args.length == 2) {
            LongWritable key = new LongWritable();
            IntWritable value = new IntWritable();
            SequenceFile.Writer writer = null;

            try {
                writer = SequenceFile.createWriter(fs, conf, path,
                        key.getClass(), value.getClass());

                int num = DATA.length;
                for (int i = 0; i < num; i++) {
                    key.set(i);
                    value.set(Integer.valueOf(DATA[i]));
                    System.out.printf("[%s]\t%s\t%s\n", writer.getLength(), key, value);
                    writer.append(key, value);
                }
            } finally {
                IOUtils.closeStream(writer);
            }

        } else if (inputfilename != null && inputfilename.length() > 0) {
            // for local file use : file:///fullpath
            String splitmark = ",";
            long lRead = readInputData(conf, fs, path, inputfilename, splitmark);
           System.out.println(" read " + lRead + " values from file:" + inputfilename);

        } else if (iRandomsize > 0) {
            writeRandomInputData(conf, fs, path, iRandomsize);
            System.out.println(" write " + iRandomsize + " MB random values in file " + outputfilename);
        } else {
            //error
            System.err.println(" wrong input parameter  !");
            System.exit(-1);
        }
         * 
         */


    }
}

